package com.sky.controller.user;

import com.sky.constant.StatusConstant;
import com.sky.entity.Setmeal;
import com.sky.result.Result;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

/**
 * @author 苏成博
 * @date 2023-02-11 19:14
 */
@RestController
@RequestMapping("/user/setmeal")
@Slf4j
@Api(tags = "用户套餐接口")
public class USetmealController {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SetMealService setMealService;
    @GetMapping("/list")
    @ApiOperation("根据分类ID查询套餐")
    @Cacheable(cacheNames = "cacheSetmeal", key = "#categoryId")//缓存注解 cacheSetmeal::100
    public Result<List<Setmeal>> findSetmealByCategoryId(Long categoryId){
        Setmeal setmeal=Setmeal.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        List<Setmeal> setmealList=setMealService.findSetmeal(setmeal);
        //List<Setmeal> setmealList= setMealService.findByCategoryId(categoryId);
        return Result.success(setmealList);
    }

    /**
     * 根据套餐id查询其关联的菜品信息
     * @param id
     * @return
     */
    @GetMapping("/dish/{id}")
    public Result<List<DishItemVO>> findSetmealDishes(@PathVariable Integer id){
        String key="dish_"+id;
        ValueOperations valueOperations = redisTemplate.opsForValue();
        List<DishItemVO> dishItemVOList = (List<DishItemVO>) valueOperations.get(key);
        if(dishItemVOList!=null && dishItemVOList.size()>0){
            return Result.success(dishItemVOList);
        }
        dishItemVOList=setMealService.findSetmealDishes(id);
        valueOperations.set(key,dishItemVOList);
        return Result.success(dishItemVOList);
    }
}
